package lc20240530;

/**
  * @description 搜索插入，返回应该插入的位置。如果相等返回索引。
  * @author 不知名帅哥
  * @date 2024/5/30 23:11
  * @version 1.0
*/
public class Search {
    public static void main(String[] args) {
        int[] nums={1,3,5,6};
        int target=2;
        System.out.println(searchInsert(nums, target));
    }
    public static int searchInsert(int[] nums, int target) {
        int left=0;
        int right= nums.length-1;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (target==nums[mid]){
                return mid;
            }else if (target>nums[mid]){
                left=mid+1;
            }else {
                right=mid-1;
            }
        }
        return left;
    }
}
